package com.platformer.model;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class Hero {
	
	private Body body;
	private Fixture physicsFixture;
	private float maxVelocity = 10f;
	
	private HeroState state;
	
	
	public Hero(World world) {
		state = HeroState.STANDING_RIGHT;
		
		BodyDef bodyDef = new BodyDef();
		bodyDef.type = BodyType.DynamicBody;
		bodyDef.position.set(0, 20);
		
		body = world.createBody(bodyDef);
		
		PolygonShape polygonShape = new PolygonShape();
		polygonShape.setAsBox(1, 1.3f);
		
		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.shape = polygonShape;
		fixtureDef.density = 1;
		
		physicsFixture = body.createFixture(fixtureDef);
		
		polygonShape.dispose();
	}
	
	public void moveLeft() {
		// apply left impulse, but only if max velocity is not reached yet
		if(body.getLinearVelocity().x > -maxVelocity) {
			body.applyLinearImpulse(new Vector2(-2f, 0), body.getWorldCenter(), true);
		}
	}
	
	public void moveRight() {
		// apply right impulse, but only if max velocity is not reached yet
		if(body.getLinearVelocity().x < maxVelocity) {
			body.applyLinearImpulse(new Vector2(2f, 0), body.getWorldCenter(), true);
		}
	}
	
	
	public Vector2 getPosition() {
		return body.getPosition();
	}
	
	public float getAngle() {
		return body.getAngle();
	}
	
	public HeroState getState() {
		return state;
	}
	
	public void setState(HeroState heroState) {
		state = heroState;
	}
	
	public Vector2 getWorldVector() {
		return body.getLinearVelocity().nor();
	}
	
//	public Vector2 getWindowPosition() {
//		Vector2 windowPosition;
//		windowPosition = new Vector2(body.getPosition().x * 10, body.getPosition().y * 10);
//		return windowPosition;
//	}

}
